﻿//------------------------------------------------------------------------------
//  
//------------------------------------------------------------------------------
#region Imported Namespaces
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;

using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.InsightUtilities;
using PHSRAG.Insight21.SearchContext;
//using Transactions = PHSRAG.Insight21.Purchasing.Transactions;
//using POAPSummaries = PHSRAG.Insight21.Purchasing.POAPSummaries;
//using POAPOrderDetails = PHSRAG.Insight21.Purchasing.POAPOrderDetails;

#endregion

namespace PHSRAG.Insight21.Purchasing.Reports {

    
    public class sqlUserOrderSummaryReport : sqlReport
    {
        private const string SearchCriteriaVersionKey = "SearchCriteriaVersion_CF49BAE7-E396-47fb-8931-9A9BC26F3602";
	    UserOrderSummaries userOrderSummaries = null;
        private const string cDomianUserID = "domainuserid";

        public sqlUserOrderSummaryReport(string pageUrl)
            : base(pageUrl)
        {
            doHashtable = true;
            doCustomExcelReport = true;
          
        }
        
        public override string ReportName {
            get {
                return "Pur_UserOrderSummaryReport";
            }
          
        }

        private string domainUserID
        {
            get
            {
                return searchCriteria["domainuserid"];
            }
           
        }

        public override Hashtable setParameterHTable(ref string rptName)
        {
            reportTitle = "User Order Summary";
            rptName = "Pur_UserOrderSummaryReport"; 
            userOrderSummaries = UserOrderSummariesInstance;
            reportSource = userOrderSummaries.GetDataContainer();

            htParameter[cDomianUserID] = domainUserID;
            return base.setParameterHTable(ref rptName);
        }

        protected override string loadCriteria(SearchCriteria searchCriteria)
        {
            //return searchCriteria.ToString();
            DateTime SalaryAsOfDate = (DateTime)((DataSet)cachePolicy.GetCachedObject(RefreshDatesControlKey)).Tables[0].Rows[0]["salaryAsOfDate"];
            DateTime glPostAsOfDate = (DateTime)((DataSet)cachePolicy.GetCachedObject(RefreshDatesControlKey)).Tables[0].Rows[0]["glPostAsOfDate"];
            DateTime POAPAsOfDate = (DateTime)((DataSet)cachePolicy.GetCachedObject(RefreshDatesControlKey)).Tables[0].Rows[0]["poapAsOfDate"];

            return searchCriteria.ToString(";") + string.Format(
                "{0}{1}"
                , "Last GL Post|" + glPostAsOfDate.ToShortDateString() + ";"
                , "Last PO/AP Post|" + POAPAsOfDate.ToShortDateString() + ";"
                //, "Last Salary Post|" + SalaryAsOfDate.ToShortDateString() + ";"
            );
        }

        private UserOrderSummaries UserOrderSummariesInstance
        {
            get
            {
                int searchVersion = searchCriteria.Version;

                UserOrderSummaries userOrderSummaries =
                    cachePolicy.GetCachedObject(UserOrderSummaries.CacheKey) as UserOrderSummaries;
                if (userOrderSummaries == null || searchVersion != SearchCriteriaVersion)
                {
                    SearchCriteriaVersion = searchVersion;
                    userOrderSummaries = new UserOrderSummaries(cachePolicy);
                    userOrderSummaries.Load(Utility.DataAccess.ConnectionString,
                        ((CommonSearchContext)cachePolicy.GetCachedObject(CommonSearchContext.CacheKey)).GetSearchCriteria(Page.Request.Url.ToString()));
                    userOrderSummaries.Cache();
                }
                return userOrderSummaries == null ? new UserOrderSummaries(cachePolicy) : userOrderSummaries;
            }
        }

        private int SearchCriteriaVersion
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.UserOrderSummaryPageKey) as Hashtable;
                return (h == null || h[SearchCriteriaVersionKey] == null) ? 0 : (int)h[SearchCriteriaVersionKey];
            }

            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.UserOrderSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SearchCriteriaVersionKey] = value;
                pageCachePolicy.Cache(Keys.UserOrderSummaryPageKey, h);
            }
        }

    }
}
